package czk.concrent.syn;

import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/7/11 10:41
 */
public class LongSync {

    public static void main(String[] args) {
        A a=new A();


        // 此时会串行执行 因为a和b是同一对象----128-127

        new Thread(() -> {
            try {
                a.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                a.b();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
    private static class A{
        Long a=Long.valueOf(1);
        Long b = Long.valueOf(1);
        public void a() throws InterruptedException {
            System.out.println(a==b);
            synchronized (a){
                System.out.println("执行a方法,5秒后退出");
                TimeUnit.SECONDS.sleep(5);
                System.out.println("a退出");
            }
        }
        public void b() throws InterruptedException {
            synchronized (b){
                System.out.println("执行a方法,2秒后退出");
                TimeUnit.SECONDS.sleep(2);
                System.out.println("b退出");
            }
        }
    }
}
